from flask import Blueprint,jsonify,render_template,request
from sql import MySql #导入全局变量
import json

bp = Blueprint("chuku",__name__,url_prefix="/chuku")

@bp.route("/add",methods=['POST'])
def add():
    param = request.json
    try:
        MySql.cursor.execute("insert into chuku (num,customer,address,tel,details) values(?,?,?,?,?);",param["record"])
        return jsonify({"message":"success","status":1})
    except:
        return jsonify({"message":"error","status":0})

@bp.route("/add.html",methods=['GET'])
def add_html():
    return render_template("chuku/add.html")

@bp.route('/search/<int:id>',methods=['GET'])
def chuku_search(id):
    MySql.cursor.execute(f"select * from world.tbchuku where id = {id};")
    row = MySql.cursor.fetchone()
    if row:
        return render_template("/chuku/ck.html",mytitle="hello linux",rows=json.loads(row["details"]),headinfo=row)
    else:
        return f"no record for id : {id}"